<?php
class Mobtel extends CI_Model {
    function __construct()
    {
        parent::__construct();
    }
    
    public function saveMobtelToDB($num, $vcode) {
		$data = array(
			'Mobtel'	=>	$num,
			'Vcode'		=>	$vcode
		);
		
		$tbl_mobtel = $this->config->item('table_mobtel');
		$query = $this->db->get_where($tbl_mobtel, array('Mobtel' => $num));
		
		// IS MERON NA IN TABLE?
		if($q = $query->result_array()) {
			// NUMBER ALREADY IN DB
			$this->db->update($tbl_mobtel, $data, array('Mobtel' => $num));
			return TRUE;
		} else {
			// INSERT NEW DATA
			$this->db->insert($tbl_mobtel, $data);
			return TRUE;
		}
		return FALSE;
   }
   
   
   public function isInMobtel($num, $vcode) {
		$data = array(
			'Mobtel'	=>	$num,
			'Vcode'		=>	$vcode
		);
		
		$tbl_mobtel = $this->config->item('table_mobtel');
		$query = $this->db->get_where($tbl_mobtel, $data);
		
		// IS MERON NA IN TABLE?
		if($q = $query->result_array()) {
			// NUMBER ALREADY IN DB
			return TRUE;
		}
		return FALSE;
   }
   
   public function isInMobtel2($num) {
		$data = array(
			'Mobtel'	=>	$num
		);
		
		$tbl_mobtel = $this->config->item('table_mobtel');
		$query = $this->db->get_where($tbl_mobtel, $data);
		
		// IS MERON NA IN TABLE?
		if($q = $query->result_array()) {
			// NUMBER ALREADY IN DB
			return TRUE;
		}
		
		return FALSE;
   }
   
    public function saveCodeToDB($mobtel, $vcode) {
		$data = array(
			'Vcode'		=>	$vcode
		);
		
		$tbl_mobtel = $this->config->item('table_mobtel');
		$query = $this->db->get_where($tbl_mobtel, array('Mobtel' => $mobtel));
		
		// IS MERON NA IN TABLE?
		if($q = $query->result_array()) {
			// NUMBER ALREADY IN DB
			$this->db->update($tbl_mobtel, $data, array('Mobtel' => $mobtel));
			return TRUE;
		} 
		return FALSE;
   }
   
    public function saveNumber() {
	
		$ret = false; 
	
		$num = $this->input->cookie('mobtel');
	    $oldnum = $this->input->cookie('oldmobtel');
		
		$data = array(
			'Mobtel'		=>	$num
		);
		
		$tbl_mobtel = $this->config->item('table_mobtel');
		$query = $this->db->get_where($tbl_mobtel, array('Mobtel' => $oldnum));
		
		// IS MERON NA IN TABLE?
		if($q = $query->result_array()) {
			// NUMBER ALREADY IN DB
			$this->db->update($tbl_mobtel, $data, array('Mobtel' => $oldnum));
			$ret = TRUE;
		}
		
		$num2 = $this->input->cookie('mobtel');
	    $oldnum2 = $this->input->cookie('oldmobtel'); 
		
		$data2 = array(
			'msisdn'		=>	$num2
		);
		
		$tbl_subscriber = $this->config->item('table_subscriber');
		$query2 = $this->db->get_where($tbl_subscriber, array('msisdn' => $oldnum2));

		if($q2 = $query2->result_array()) {
			// NUMBER ALREADY IN DB
			$this->db->update($tbl_subscriber, $data2, array('msisdn' => $oldnum2));
			$ret = TRUE;
		} 		
		return $ret;
   }
   
   public function getVcode($mobtel) {
		$data = array(
			'Mobtel'		=>	$mobtel
		);
		
		$tbl_mobtel = $this->config->item('table_mobtel');
		$query = $this->db->get_where($tbl_mobtel, array('Mobtel' => $mobtel));
		
		// IS MERON NA IN TABLE?
		if($q = $query->result_array()) {
			return $q;
		} 
		return FALSE;   	
   }
   
   public function saveNonGlobe() {
		$subsId = $this->input->cookie('subsId');
		$data = array(
			'msisdn'		=>	"0"
		);
		// NUMBER ALREADY IN DB
		$tbl_subscriber = $this->config->item('table_subscriber');
		if($this->db->update($tbl_subscriber, $data, array('subsId' => $subsId))) {
			return TRUE;
		}
		return FALSE;
   }
}